package src.Players;

import src.Main;

public class PlayerTrade {
	private Player p;
	private PlayerItems pi;

	public PlayerTrade(Player _p) {
		p = _p;
	}
	
	public void tradePlayer() {
		if(p == null) {
			return;
		}
		if(p.inTrade == true || p.inDuel == true) {	
			return;
		}	
		Player plr = Main.m.ge.players[p.tradePlayer];
		p.tradingWith = plr.playerIndex;
        	if(p.faceToRequest != (p.tradePlayer + 32768)) {
           		p.requestFaceTo(p.tradePlayer + 32768);
        	}
		if(plr != null) {
			if(Main.m.ge.getDistance(plr.absX, plr.absY, p.absX, p.absY) != 1) {
                    			return;
                	}	
			if(plr.tradingPlayer == true && plr.tradingWith == p.playerIndex) {
				plr.tradingPlayer = false;
				plr.inTrade = true;
				p.tradingPlayer = false;
				p.inTrade = true;
				plr.frame.showInterface(plr, 3323, 5063);
				p.frame.showInterface(p, 3323, 5063);
				p.frame.resetItems(p, 5064);
				plr.frame.resetItems(plr, 5064);
				p.frame.setText126(p, "Trading with: "+plr.username, 3417);
				p.frame.setText126(p, "", 3431);
				plr.frame.setText126(plr, "Trading with: "+p.username, 3417);
				plr.frame.setText126(plr, "", 3431);
			} else {
				p.frame.sendMessage(p, "Trade request sent...");
				plr.frame.sendMessage(plr, p.username+" wants to trade.");
			}			
		}
	}

	public void acceptFirstScreen(Player p) {
		if(p == null) {
			return;
		}
		Player plr = Main.m.ge.players[p.tradePlayer];
		p.tradingWith = plr.playerIndex;
		if(plr != null) {
			p.acceptedFirstScreen = true;
			p.frame.setText126(p, "Waiting on other player...", 3431);
			plr.frame.setText126(plr, p.username+" has accepted.", 3431);
			if(plr.acceptedFirstScreen == true) {
				p.acceptedFirstScreen = false;
				plr.acceptedFirstScreen = false;
				p.frame.removeAllWindows(p);
				plr.frame.removeAllWindows(plr);
				p.frame.showInterface(p, 3443, 3213);
				plr.frame.showInterface(plr, 3443, 3213);			
			}
		}
	}

	public void acceptSecondScreen(Player p) {
		if(p == null) {
			return;
		}
		Player plr = Main.m.ge.players[p.tradePlayer];
		p.tradingWith = plr.playerIndex;
		if(plr != null) {
			p.acceptedSecondScreen = true;
			if(plr.acceptedSecondScreen == true) {
				p.acceptedSecondScreen = false;
				plr.acceptedSecondScreen = false;
				// add items
				p.frame.removeAllWindows(p);
				plr.frame.removeAllWindows(plr);
				p.inTrade = false;
				plr.inTrade = false;
			}
		}
	}

	public boolean appendItem(Player p, int itemId, int fromSlot, int amt) {
		Player plr = Main.m.ge.players[p.tradePlayer];
		if(p == null || fromSlot < 0 || amt <= 0) {
			return false;
		}
		PlayerChecks pc = new PlayerChecks(p);
		if(!pc.playerHasItem(p, itemId)) {
			return false;
		}
		int toSlot = getEmptySlot(p);
		if(pi.invItemCount(p, itemId) > 0) {
			pi.playerTItems[toSlot] = p.items[pi.getItemSlot(p, itemId)];
			pi.playerTItemsN[toSlot]++;
			pi.deleteItem(p, (p.items[pi.getItemSlot(p, itemId)] - 1), pi.getItemSlot(p, itemId), 1);
			amt--;
		}
		p.frame.resetItems(p, 5064);
		p.frame.resetTItems(p, 3323);
		p.frame.setText126(p, "", 3431);
		plr.frame.setText126(plr, "", 3431);
		return true;	
	}

	private int freeSlots(Player p) {
		if(p == null) {
			return -1;
		}
		int s = 0;
		for(int i = 0; i < 28; i++) {
			if(pi.playerTItems[i] <= 0) {
				s++;
			}
		}
		return s;
	}

	private int getEmptySlot(Player p) {
		if(p == null) {
			return -1;
		}
		for(int i = 0; i < 28; i++) {
			if(pi.playerTItems[i] <= 0) {
				return i;
			}
		}
		return -1;
	}

	private int tradeItemSlot(Player p, int id) {
		if(p == null) {
			return -1;
		}
		for(int i = 0; i < 28; i++) {
			if(pi.playerTItems[i] == (id + 1)) {
				return i;
			}
		}
		return -1;
	}

	private int tradeItemAmount(Player p, int id) {
		int amt = 0;
		if(p == null || id < 0) {
			return 0;
		}
		for(int i = 0; i < 28; i++) {
			if(pi.playerTItems[i] == (id + 1)) {
				amt += pi.playerTItemsN[i];
			}
		}
		return amt;
	}
}